package com.yongcheon.sms_manager.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.Telephony;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.activities.DeleteNotifationActivity;
import com.yongcheon.sms_manager.activities.PreferenceSettingActivity;

/**
 * Created by yongcheon on 2014-12-04.
 */
public class DefaultAppUtil {
    public static final String SHARD_PREFERENCE_NAME = "pref";
    public static final String DEFAULT_PKG_NAME = "default_pkg_name";
    public static final int REQ_DEFAULT_SMS_CHG  = 1;
    public static final int REQ_FLAG_NO_RESULT  = 1;
    public static final int REQ_FLAG_RESULT  = 2;

    public static boolean checkIsDefault(Context context){
        boolean returnBoolean = true;
        if(AndroidSystemUtil.ANDROID_VERSION > 16) {
            String myPackageName = context.getPackageName();
            String defaultPkgName = Telephony.Sms.getDefaultSmsPackage(context);
            returnBoolean = defaultPkgName.equals(myPackageName);
        }
        return returnBoolean;
    }

    public static void setDefaultAppSHow(final Context context, final int flag, final int reqCode, final Object callObj){
        new AlertDialog.Builder(context)
        .setTitle(context.getResources().getString(R.string.default_dialog_title))
        .setMessage(context.getResources().getString(R.string.default_dialog_setting_msg))
        .setPositiveButton(context.getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (flag ==  REQ_FLAG_NO_RESULT) {
                    setDefaultApp(context);
                }else if(flag ==  REQ_FLAG_RESULT){
                    setDefaultAppResult((Activity)context, reqCode);
                }
            }
        })
        .setNegativeButton(context.getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               if(callObj instanceof DeleteNotifationActivity){
                   DeleteNotifationActivity activity = (DeleteNotifationActivity)callObj;
                   activity.deleteNoti();
               }
            }
        }).show();
    }

    public static void setDefaultApp(Context context){
        saveSMSDefaultPkgName(context);
        Intent intent = createDefaultSMSChgIntent(context);
        context.startActivity(intent);
    }

    public static void setDefaultAppResult(Activity activity, int reqCode){
        saveSMSDefaultPkgName(activity);
        Intent intent = createDefaultSMSChgIntent(activity);
        activity.startActivityForResult(intent,reqCode);
    }

    public static Intent createDefaultSMSChgIntent(Context context){
        String myPackageName = context.getPackageName();
        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName);
        return intent;
    }

    public static void saveSMSDefaultPkgName(Context context){
        String defaultPkgName = Telephony.Sms.getDefaultSmsPackage(context);
        SharedPreferences pref = context.getSharedPreferences(SHARD_PREFERENCE_NAME, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(DEFAULT_PKG_NAME, defaultPkgName);
        editor.commit();
    }

    public static void restoreDefaultApp(Context context){
        SharedPreferences pref = context.getSharedPreferences(SHARD_PREFERENCE_NAME, context.MODE_PRIVATE);
        String newDefaultPkgName = pref.getString(DEFAULT_PKG_NAME, "");
        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, newDefaultPkgName );
        context.startActivity(intent);
    }

    public static boolean isWritable(Context context){
        boolean returnBoolean = false;
        if (DefaultAppUtil.checkIsDefault(context)) {
            returnBoolean = true;
        } else {
            DefaultAppUtil.setDefaultAppSHow((Activity) context, DefaultAppUtil.REQ_FLAG_NO_RESULT, -1, null);
        }
        return returnBoolean;
    }

    public static String getDefaultAppPkgName(Context context){
        SharedPreferences pref = context.getSharedPreferences(SHARD_PREFERENCE_NAME, context.MODE_PRIVATE);
        String defaultPkgName = pref.getString(DEFAULT_PKG_NAME, "");
        return defaultPkgName;
    }

    public static boolean isEnbleSMSBlick(Context context){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getBoolean(PreferenceSettingActivity.SPAM_SMS_BLOCK_ENABLE, false);
    }

    public static boolean isEnblePhoneBlick(Context context){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getBoolean(PreferenceSettingActivity.SPAM_PHONE_BLOCK_ENABLE, false);
    }
}
